<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class m_soal extends CI_Model {

    function ambil_judul($id) {
        $query = $this->db->get_where('tb_jenis', array('id_jenis' => $id));
        return $query;
    }

    function ambil_soal($id, $limit, $offset) {
        $query = $this->db->get_where('tb_soal', array('id_jenis' => $id), $limit, $offset);
        return $query;
    }
    
    function get_id_soal($txt_soal, $imgname){
        $query = $this->db->get_where('tb_soal', array('text_soal' => $txt_soal, 'filegambar' => $imgname))->row();
        return $query;
    }

    function tambah_soal($txt, $id, $imgname) {
        $this->db->insert('tb_soal', array('text_soal' => $txt, 'id_jenis' => $id, 'filegambar' => $imgname));
    }

    function tambah_jawaban($t, $v, $i, $imgname) {
        $data = array(
            'id_jawaban' => NULL,
            'text_jawaban' => $t,
            'values' => $v,
            'id_soal' => $i,
            'filegambar' => $imgname
        );
        $query = $this->db->insert('tb_jawaban', $data);
        return $query;
    }

    function edit_soal($a) {
        $d = $this->db->get_where('tb_soal', array('id_soal' => $a))->row();
        return $d;
    }
    
    function edit_jawaban($id_soal){
        $query = $this->db->get_where('tb_jawaban', array('id_soal'=> $id_soal));
        if($query->num_rows() > 0){
            foreach($query->result() as $data){
                $hasil[] = $data;
            }
            return $hasil;
        }
        
    }
    
    function update_soal($id, $txt){
        $data = array('text_soal' => $txt);
        $this->db->where('id_soal', $id);
        $this->db->update('tb_soal', $data);
    }
    
    function update_jawaban($id_jawaban, $txt, $val){
        $data = array(
            'text_jawaban' => $txt,
            'values'=> $val);
        $this->db->where('id_jawaban', $id_jawaban);
        $this->db->update('tb_jawaban', $data);
    }


    function hapus($a){
        $this->db->delete('tb_jawaban', array('id_soal' => $a));
        $this->db->delete('tb_soal', array('id_soal' => $a));
        return;
    }
    
    function cari_like($kata, $id) {
        $this->db->like('text_soal', $kata);
        $q = $this->db->get_where('tb_soal', array('id_jenis' => $id));
        return $q;
    }
    
   function cari($kata, $id, $limit, $offset) {
        $this->db->like('text_soal', $kata);
        $query = $this->db->get_where('tb_soal', array('id_jenis' => $id), $limit, $offset);
        return $query;
    }
    
    
    function update_image_soal($id, $imgname){
        $data = array('filegambar' => $imgname);
        $this->db->where('id_soal', $id);
        $this->db->update('tb_soal', $data);
    }
    
    function update_image_jawaban($id, $imgname){
        $data = array('filegambar' => $imgname);
        $this->db->where('id_jawaban', $id);
        $this->db->update('tb_jawaban', $data);
    }
    
        //function ambil buat xml-rpc
    
     function ambil($id, $limit, $offset) {
        $ambil = $this->db->get_where('tb_soal', array('id_jenis' => $id), $limit, $offset);
        if ($ambil->num_rows() > 0) {
            foreach ($ambil->result() as $data) {
                $hasil[] = $data;
            }
            return $hasil;
        }
    }
    
    function ambil_jawaban($id, $limit, $offset){
        $ambil = $this->db->get_where('tb_jawaban', array('id_soal' => $id), $limit, $offset);
        if($ambil->num_rows() > 0){
            foreach ($ambil->result() as $data){
                $hasil[] = $data;
            }
            return $hasil;
        }
        
    }
    
    function get_image(){
        $sql = "SELECT a.filegambar AS gambarsoal, b.filegambar AS gambarjawaban FROM tb_soal a INNER JOIN tb_jawaban b ON a.id_soal=b.id_soal ";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

}

?>
